package com.ludomancy.gamelib;

import java.awt.Graphics;

/*
 * Created on Sep 4, 2003
 */

/**
 * @author Administrator
 *
 * This class can iterate over the images of an ImageStrip.
 */
public class ImageAnimation 
{
	public ImageStrip strip = null;
	public boolean looping = false;
	public int [] sequence = null;

	public int frameTime;
	private int currentTime = 0;
	public int currentFrame = 0;
	private boolean playing = false;

	public static ImageAnimation create(String file, int frameWidth, int frameHeight, int frameTime)
	{
		return new ImageAnimation(new ImageStrip(ResourceManager.getImage(file), frameWidth,frameHeight), frameTime);
	}
	
	public ImageAnimation(ImageStrip imageStrip, int frameTime) 
	{	
		int[] seq = new int[imageStrip.getImageCount()];
		for(int i = 0 ; i != seq.length ; i++)
			seq[i] = i;
		commonConstruct(imageStrip, frameTime, seq);
	}

	public ImageAnimation(ImageStrip imageStrip, int frameTime, int [] sequence) 
	{
		commonConstruct(imageStrip,frameTime,sequence);
	}
	
	private void commonConstruct(ImageStrip sourceStrip, int frameDuration, int [] sequenceOfFrames)
	{
		this.frameTime = frameDuration;
		this.strip = sourceStrip;
		this.sequence = sequenceOfFrames;		
	}
	
	public void update() 
	{	
		if (this.playing) 
		{
			this.currentTime ++;
			if (this.currentTime >= this.frameTime) 
			{
				advance();
				this.currentTime = 0;
			}
		}
	}
	
	public void setSequenceBouncingLoop()
	{
		int[] bounce = new int[sequence.length*2];
		int ndx = 0;
		for (int i = 0; i < sequence.length; i++)
		{
			bounce[ndx++] = sequence[i];
		}
		for (int i = 0; i < sequence.length; i++)
		{
			bounce[ndx++] = sequence[sequence.length - i -1];
		}
		sequence = bounce;
	}
	
	public void play()
	{
		this.playing = true;
	}
	
	public void stop() 
	{	
		this.playing = false;
	}
	
	public void pause() 
	{	
		this.playing = false;
	}
	
	public int[] getBitmask(int x, int y, int w, int h)
	{
		return strip.getBitmask(currentFrame, x, y, w, h);
	}
	
	public void advance() 
	{
		this.currentFrame++;
		if (this.currentFrame >= this.sequence.length)
		{
			if( this.looping )
				this.currentFrame = 0;
			else
			{
				stop();
				this.currentFrame = this.sequence.length-1;
			}
		}
	}
	
	public void setCurrentFrame(int frame) 
	{	
		this.currentFrame = frame;
	}
	
	public void rewind() 
	{	
		this.currentFrame = 0;
	}
	
	public void paintCurrentFrame(Graphics g, int x, int y)
	{
		strip.paint(g, this.sequence[this.currentFrame], x, y);
	}
	
	public void paintCurrentFrame(Graphics g, int x, int y, int anchor)
	{
		strip.paint(g, this.sequence[this.currentFrame], x, y, anchor);
	}
	
	public boolean isOver()
	{
		return currentFrame == sequence.length-1 || playing == false;
	}
	
	public void reverse()
	{
		for (int i = 0; i < sequence.length; i++)
		{
			sequence[i] = sequence.length-i-1;
		}
	}

	public void setSequence(int[] sequence) 
	{
		this.sequence = sequence;
		this.currentFrame = 0;
	}
}
